package com.czf.personal.controller;

import com.czf.common.entity.CreditUser;
import com.czf.common.resp.LoginResponse;
import com.czf.common.util.AjaxResult;
import com.czf.personal.service.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Author chenzhanfan
 * @Date 2022/3/17
 * @Description 用户注册和登录
 */
@RestController
//@CrossOrigin
@Slf4j
@Api(tags = "用户注册与登录")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;



    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ApiOperation(value = "用户注册")
    public AjaxResult register(@RequestBody CreditUser user){
        int i = userService.registerUser(user);
        if (i == 1) {
            return AjaxResult.success("注册成功",i);
        }
        return AjaxResult.error("注册失败");
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ApiOperation(value = "用户登录")
    public AjaxResult login(CreditUser user){
        String account = user.getAccount();
        String password = user.getPassword();
        LoginResponse response = userService.loginUser(account, password);
        if (ObjectUtils.isNotEmpty(response)) {
            return AjaxResult.success("登录成功", response);
        } else {
            return AjaxResult.error("账号或密码错误");
        }
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    @ApiOperation(value = "用户登出")
    public AjaxResult logout(HttpServletRequest request){
        int logout = userService.logout(request);
        if (logout == 1) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequestMapping(value = "getInfo",method = RequestMethod.GET)
    public AjaxResult getInfo(Long userId){
        CreditUser info = userService.getInfo(userId);
        if (ObjectUtils.isNotEmpty(info)){
            return AjaxResult.success(info);
        }
        return AjaxResult.error();
    }
}
